import { Component, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { SettimeComponent } from '../settime/settime.component';
import { CardAddComponent } from '../card-add/card-add.component';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';
import { ACLService } from '@delon/acl';

@Component({
  selector: 'user-list',
  templateUrl: './card-list.component.html',
  styles: []
})
export class CardListComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  frozenFlag:boolean = this.aclService.canAbility("door-card/frozen");//检查用户按钮权限
  setFlag:boolean =  this.aclService.canAbility("door-card/set");
  cancelFlag:boolean =  this.aclService.canAbility("door-card/cancel");
  delFlag:boolean =  this.aclService.canAbility("door-card/delete");
  addFlag:boolean =  this.aclService.canAbility("door-card/add");
  unfrozenFlag:boolean =  this.aclService.canAbility("door-card/unfrozen");
  selectData: any[];
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  config: SPage = {
    columns: [
      // {
      //   index: 'id',
      //   title: '多选',
      //   type: ColumnType.checkbox,
      // },
      {
        index: 'id',
        title: '卡号',
        // fFn: (item, columu) => this.delCardZero(item.cardNo),
        // filter: {
        //   placeholder: '请输入卡号',
        //   type: 'string'
        // }
      },
      {
        index: 'cardNo',
        title: '虚拟卡号',
        //fFn: (item, columu) => this.delCardZero(item.cardNo),
        filter: {
          placeholder: '请输入虚拟卡号',
          type: 'string'
        }
      },
      {
        index: "cardCode",
        title:"卡编号",
        filter: {
          placeholder:'请输入卡编号',
          type:'string'
        }
      },
      {
        index: 'cardType',
        title: '门卡类型',
        type: ColumnType.tag,
        tag: {
          1:{text:'IC卡'},
          2:{text:'CPU卡'},
          3:{text:'身份证'},
          4:{text:'居住证'},
          5:{text:'银行卡'},
          6:{text:'APP'},
        },
        filter: {
          placeholder: '请选择门卡类型',
          type: 'select',
          items: [
            {label: 'IC卡', value: 1},
            {label: 'CPU卡', value: 2},
            {label: '身份证', value: 3},
            {label: '居住证', value: 4},
            {label: '银行卡', value: 5},
            {label: 'APP', value: 6},
          ]
        }
      },
      {
        index:'cardStatus',
        title:'门卡状态',
        type: ColumnType.tag,
        tag:{
          0:{text:'未激活',color: 'red'},
          1:{text:'已激活', color: 'green'},
          2:{text:'丢失', color: 'red'},
          3:{text:'损坏', color: 'red'},
          4:{text:'冻结', color: 'red'},
          5:{text:'待缴费', color: 'red'},
          6:{text:'已过期', color: 'red'},
          7:{text:'退卡', color: 'red'},
          9:{text:'其他'}
        },
        filter: {
          placeholder: '请选择门卡状态',
          type: 'select',
          items:[
            {label:'未激活',value: 0},
            {label:'已激活', value: 1},
            {label:'丢失', value: 2},
            {label:'损坏', value: 3},
            {label:'冻结', value: 4},
            {label:'待缴费', value: 5},
            {label:'已过期', value: 6},
            {label:'退卡', value: 7},
            {label:'其他', value: 9}
          ]
        }
      },
      {
        index: 'cardUsingStartTime',
        title: '最初使用时间'
      },
      {
        index: 'cardUsingEndTime',
        title: '最后使用时间'
      },
      {
        index:'cardRemark',
        title:'备注'
      },
    ],
    showCheckBox: true,
    reqUrl:'/core/door/card/page',
    // editBtn: [
    //   { title: '设置' , iif: () => this.setFlag},
    //   { title: '激活', iif: () => this.unfrozenFlag},
    //   { title: '冻结', iif: () => this.frozenFlag},
    //   { title: '退卡', iif: () => this.cancelFlag},
    //   { title: '删除', iif: () => this.delFlag}
    // ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService, private message: NzMessageService,private aclService: ACLService) { }
  
    //删除卡号后面的8个0
    delCardZero(num:string):string{
      if(num.length > 8){
        num = num.substring(0,num.length - 8);
      }
      return num;
    }

  ngOnInit() {

  }

  // 编辑功能开始
  editDic(type: 0 | 1 = 0, _param?: object): void {
    console.log(type);
    const modal = this.modal.create({
      nzTitle: '设置卡片有效期',
      nzContent: SettimeComponent,
      nzWidth: 800,
      nzComponentParams: {
        type,
        _param
      },
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }
  //冻结卡片
  fCard(param:number):void{
    console.log(param);
    const body = [];
    body.push({id:param,status:false,cardStatus:3});
    this.http.put('/core/door/card/batch',body).subscribe(res => {
      if(res.status === '200'){
        this.message.success('冻结成功');
        this.st.reload();
      }
    })
  }
  //激活卡片
  aCard(param:number):void{
    const body = {status:true,cardStatus:1};
    this.http.put('/core/door/card/'+param,body).subscribe(res => {
      if(res.status === '200'){
        this.message.success('激活成功');
        this.st.reload();
      }
    })
  }
  //退卡
  rCard(param:number):void{
    const body = {status:false,cardStatus:4};
    this.http.put('/core/door/card/'+param,body).subscribe(res => {
      if(res.status === '200'){
        this.message.success('退卡成功');
        this.st.reload();
      }
    })
  }
  //新增门卡
  addDoorCard(): void {
    const modal = this.modal.create({
      nzTitle: '新增门卡',
      nzContent: CardAddComponent,
      nzWidth: 800,
      nzOnOk: () => {
        this.st.reload();
      },
      nzFooter: null
    })
    modal.afterClose.subscribe(res => {
      if (res === 'ok') {
        this.st.reload();
      }
    })
  }

  operate(e: any): void {
    if (e.title === '设置') {
      this.editDic(1, e.data);
    }else if(e.title === '冻结'){
      this.fCard(e.data.id);
    }else if(e.title === '激活'){
      this.aCard(e.data.id);
    }else if(e.title === '退卡'){
      this.rCard(e.data.id);
    }else if(e.title === '删除'){
      this.modal.confirm({
        nzTitle: '确定删除?',
        nzOnOk: () => {
          const body = [];
          // this.selectData.forEach(item => {
          //   body.push({id: item.id})
          // })
          this.http.put(this.api.user.delete.url,body).subscribe(res => {
            if (res.status === '200') {
              this.message.success('删除成功');
              this.st.reload();
            }
          })
        }
      })
    }
  }
  _console(e: any): void {
    this.selectData = e;
  }
  //冻结多条
  fCards() {
    if (!!this.selectData && this.selectData.length !== 0) {
      this.modal.confirm({
        nzTitle: '您确定要冻结这些数据吗',
        nzOnOk: () => {
          const body = [];
          this.selectData.forEach(item => {
            body.push({id:item.id,status:false,cardStatus:3});
          });
          this.http.put('/core/door/card/batch',body).subscribe(res => {
            if(res.status === '200'){
              this.message.success('冻结成功');
              this.st.reload();
            }
          })
        }
      })
    } else {
      this.message.warning('请选择数据');
    }
  }
  

}
